#include "sensor.h"

extern unsigned short DAC_Value[23]={0,1490,1540,1575,1605,1635,1750,1855,1950,2030,2105,2185,2245,2300,2360,2410,2480,2535,2620,2730,2785,2860,3100};
const unsigned short flow_value[91]={650,701,759,822,897,976,1041,1106,1172,1237,1302,1366,1429,1493,1556,1620,1671,1722,1773,1824,1875,1918,1961,2004,2047,2090,2127,2164,2202,2239,2276,2316,2357,2397,2438,2478,2508,2538,2567,2597,2627,2656,2685,2715,2744,2773,2803,2834,2864,2895,2925,2946,2966,2987,3007,3028,3056,3084,3113,3141,3169,3188,3207,3226,3245,3264,3277,3290,3302,3315,3328,3346,3364,3381,3399,3417,3434,3450,3467,3483,3500,3509,3518,3527,3536,3545,3556,3567,3578,3589,3600};
                                  //{640,675,725,780,845,910,967,1024,1081,1138,1195,1249,1303,1357,1411,1465,1512,1559,1606,1653,1700,1740,1780,1820,1860,1900,1933,1966,1999,2032,2065,2103,2140,2178,2215,2253,2280,2308,2335,2363,2390,2418,2446,2474,2502,2530,2556,2582,2608,2634,2660,2686,2712,2738,2764,2790,2816,2842,2868,2894,2920,2938,2956,2974,2992,3010,3028,3046,3064,3082,3100,3112,3124,3136,3148,3160,3170,3180,3190,3200,3210,3220,3230,3240,3250,3260,3268,3276,3284,3292,3300};//
const unsigned short flow_value2[91]={618,685,745,809,873,939,1002,1065,1128,1191,1254,1318,1382,1446,1510,1574,1624,1674,1725,1775,1825,1866,1907,1948,1989,2030,2069,2108,2148,2187,2226,2263,2300,2336,2373,2410,2445,2480,2514,2549,2584,2616,2648,2679,2711,2743,2776,2809,2843,2876,2909,2928,2946,2965,2983,3002,3032,3061,3091,3120,3150,3177,3203,3230,3256,3283,3297,3311,3326,3340,3354,3377,3399,3422,3444,3467,3489,3511,3532,3554,3576,3590,3604,3617,3631,3645,3656,3667,3678,3689,3700};
                                   //{608,635,685,740,805,890,945,1000,1055,1110,1165,1217,1269,1321,1373,1425,1470,1515,1560,1605,1650,1688,1726,1764,1802,1840,1877,1914,1951,1988,2025,2062,2099,2136,2173,2210,2238,2266,2294,2322,2350,2383,2416,2449,2482,2515,2538,2561,2584,2607,2630,2656,2682,2708,2734,2760,2786,2812,2838,2864,2890,2914,2938,2962,2986,3010,3028,3046,3064,3082,3100,3112,3124,3136,3148,3160,3170,3180,3190,3200,3210,3220,3230,3240,3250,3260,3268,3276,3284,3292,3300};

unsigned char AD2PRESS(int);
unsigned char AD2PRESS2(int);
unsigned char AD2flow(int);
unsigned char AD2flow2(int);
int PRESS2AD(unsigned char);
int PRESS2AD2(unsigned char);
char AD2O2(unsigned char);

//float INSP_PRESS_BASE=833.0;
//float EXP_PRESS_BASE=850.0;

unsigned char AD2flow(int flow_sam)
{
	unsigned char num;
	if(flow_sam<FLOW_BASE+flow_value[1])
		return 0;
	else
	{
		for(num=1;num<92;num++)
			if(flow_sam<FLOW_BASE+flow_value[num])
				return (num-1);
		return 90;
	}
}	

/*
   int AD2flow(int flow_sam)
   {
   double flow_value;
   float x;

   x=flow_sam/100;
   if (flow_sam<=640)
   flow_value=0;
   else
   if ((640<flow_sam)&&(flow_sam<=910))
   flow_value=-0.223*x*x+5.2607*x-24.4506;
   else
   if ((910<flow_sam)&&(flow_sam<=2253))
   flow_value=0.0579*x*x+0.3995*x-3.3067;
   else
   if ((2253<flow_sam)&&(flow_sam<=2790))
   flow_value=0.0047*x*x*x-0.3216*x*x+10.91*x-101.5;
   else
   if ((2790<flow_sam)&&(flow_sam<=3300))
   flow_value=0.1131*x*x*x-9.4359*x*x+266.1832*x-2482.7;
   else
   flow_value=90;
   return(flow_value+0.3);                   
   }
   */

/*
   unsigned char AD2flow2(int flow2_sam)
   {
   double flow_value;
   if(flow2_sam<640)
   flow_value=0;
   else
   if(flow2_sam<890)
   flow_value=flow_value=-0.223*(flow2_sam/100)*(flow2_sam/100)+5.2607*(flow2_sam/100)-24.4506;
   else
   flow_value=0.0921*(flow2_sam/100)*(flow2_sam/100)-0.5879*(flow2_sam/100)+2.9127;

   if(flow_value>90)
   flow_value=90;

   return(flow_value+0.3);
   }
   */

unsigned char AD2flow2(int flow_sam2)
{
	unsigned char num2;
	if(flow_sam2<FLOW_BASE2+flow_value2[1])
		return 0;
	else
	{
		for(num2=1;num2<92;num2++)
			if(flow_sam2<FLOW_BASE2+flow_value2[num2])
				return (num2);
		return 90;
	}
}

int PRESS2AD(unsigned char pre_value)
{
	return(INSP_KPA*pre_value+INSP_PRESS_BASE);
}

int PRESS2AD2(unsigned char pre_value2)
{
	return(EXP_KPA*pre_value2+EXP_PRESS_BASE);
}

unsigned char AD2PRESS(int pre_AD)
{
	if(pre_AD<INSP_PRESS_BASE-10.0*INSP_KPA)
		return 0;
	else
		return(3.0*(pre_AD-INSP_PRESS_BASE)/INSP_KPA+30.0);
}

unsigned char AD2PRESS2(int pre_AD2)
{
	if(pre_AD2<EXP_PRESS_BASE-10.0*EXP_KPA)
		return 0;
	else
		return(3.0*(pre_AD2-EXP_PRESS_BASE)/EXP_KPA+30.0);
}


char AD2O2(unsigned char O2_value)
{
	if(O2_value<57)
		return(21);
	else
		return((O2_value-4)*0.3962+0.5);
}
